<!--
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2022-04-12 18:47:17
 * @LastEditTime: 2023-02-23 11:41:48
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description: 新建 / 编辑 页面
 * @FilePath: \vue3-element-plus-web\src\views\SysManage\Dept\components\Drawer\create.vue
 * Copyright 版权声明
-->
<template>
  <div class="">
    <el-form
      :rules="rules"
      ref="dataForm"
      :model="temp"
      label-width="100px"
      style='width:100%;'
    >

      <el-form-item label="父部门名称：" v-if="pageParams.parentName">
        {{pageParams.parentName}}
      </el-form-item>

      <el-form-item label="部门编码：" prop="deptCode">
        <el-input v-model="temp.deptCode" placeholder="请输入部门编码"></el-input>
      </el-form-item>

      <el-form-item label="部门名称：" prop="deptName">
        <el-input v-model="temp.deptName" placeholder="请输入部门名称"></el-input>
      </el-form-item>

      <el-form-item label="部门全称：" prop="fullName">
        <el-input v-model="temp.fullName" placeholder="请输入部门全称"></el-input>
      </el-form-item>

      <el-form-item label="备注：" prop="remark">
        <el-input v-model="temp.remark" placeholder="请输入备注"></el-input>
      </el-form-item>

      <el-form-item label="序号：" prop="sort">
        <el-input v-model="temp.sort" placeholder="请输入序号"></el-input>
      </el-form-item>

      <el-form-item label="状态：" prop="status">
        <el-radio-group v-model="temp.status">
          <el-radio
            v-for="item in stateList"
            :key="item.value"
            :label="item.value">
            {{item.label}}
          </el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { ENABLED_STATE_ENUM } from '@/config'
import { positiveInteger } from '@/utils/validate'

export default {
  name: 'Create',
  computed: {
    ...mapGetters([
      'drawerCurrent'
    ]),
    drawerType: function() {
      return this.drawerCurrent.drawerType
    }
  },
  components: {
  },
  data() {
    // 校验正整数
    const versionNum = (rule, value, callback) => {
      value = value * 1
      if (value === 0) {
        callback()
      } else {
        if (!positiveInteger(value)) {
          callback(new Error('请输入小于11位的整数'))
        } else {
          callback()
        }
      }
    }
    return {
      pageParams: {},
      temp: {
        status: 1
      },
      rules: {
        deptCode: [
          { required: true, message: '请输入部门编码', trigger: 'change' },
          { max: 50, message: '部门编码最大长度50位', trigger: 'change' }
        ],
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'change' },
          { max: 50, message: '部门名称最大长度50位', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入序号', trigger: 'change' },
          { required: true, validator: versionNum, trigger: 'change' }
        ]
      },
      // 状态列表
      stateList: ENABLED_STATE_ENUM,
      // 暂存当前行源数据
      rowBAK: {}
    }
  },
  watch: {
    // 表单内容体
    temp: {
      handler: function(val, oldVal) {
      },
      deep: true
    }
  },
  created() {
    // 列表传过来的数据
    const temp = this.drawerCurrent.params.row
    this.temp = Object.assign({}, this.temp, temp)
    this.pageParams = this.drawerCurrent.params.pageParams || {}
    if (this.pageParams.parentId) {
      this.temp.parentId = this.pageParams.parentId
    }
    // *******************如果是 【编辑】 需要从接口数据的话执行一下操作
    if (this.drawerType === 'UPDATE') {
      // drawer loadding 状态 打开
      this.$store.dispatch('SetDrawerloading', true)
      setTimeout(() => {
        // drawer loadding 状态 关闭
        this.$store.dispatch('SetDrawerloading', false)
      }, 500)
    }
    if (this.drawerType === 'CREATE') {
      this.rowBAK = JSON.parse(JSON.stringify(Object.assign({}, this.pageParams.rowBAK)))
    }
  },
  mounted() {

  },
  methods: {
    // 获取行数据
    getRowBAK() {
      return this.rowBAK
    },
    // 提交表单
    submitForm() {
      return this.$refs['dataForm'].validate()
    },
    // 获取表单数据
    getFormData() {
      return this.temp
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
